package com.tuzhi.filestore.websocket.server;

import com.alibaba.fastjson2.JSONObject;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.filestore.http.BillApiClient;
import com.tuzhi.filestore.websocket.vo.MessageVo;
import com.tuzhi.filestore.websocket.vo.OrderStatusMsgVo;
import com.tuzhi.vo.bill.FilesModelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class BillAppService {
    @Resource
    BillApiClient billApiClient;

    //前端模拟打开报价专用websocket专用客户端通知到自检结果
    public void updateFileModel(MessageVo messageVo) {
        FilesModelVo filesModelVo = new FilesModelVo();
        FilesModelVo.ModelVo modelVo = filesModelVo.new ModelVo();
        filesModelVo.setData(modelVo);
        modelVo.setStatus("processed");
        JSONObject jsonObject = (JSONObject) messageVo.getData();
        Long modelId = jsonObject.getLong("modelId");
        String result = jsonObject.getString("checkResult");
        log.info("前端模拟打开报价专用websocket专用客户端通知到自检结果，modelId= {} ，结果={}", modelId, result);
        AjaxResultVO ajaxResultVO = billApiClient.update(filesModelVo, modelId);
        if (ajaxResultVO != null && "0".equals(ajaxResultVO.getCode())) {
            OrderStatusMsgVo orderStatusMsgVo = new OrderStatusMsgVo();
            orderStatusMsgVo.setStatus(filesModelVo.getData().getStatus());
            orderStatusMsgVo.setModelId(modelId);
            try {
                WebSocketHelp.sendOrderStatus(orderStatusMsgVo);
            } catch (Exception e) {
                log.error("异步调用通知前端订单状态发生变化 失败", e);
            }
            log.info("异步调用通知前端订单状态发生变化 完成");
        } else {
            log.info("根据前端模拟打开报价专用websocket专用客户端通知到自检结果,更新模型状态失败：{}", ajaxResultVO.getMsg());
        }
    }
}
